package com.atguigu.java;

import java.util.concurrent.locks.ReentrantLock;

public class BankTest1 {
    public static void main(String[] args) {
        Bank1 b = new Bank1();
        Thread t1 = new Thread(b);
        Thread t2 = new Thread(b);

        t1.setName("甲");
        t2.setName("乙");

        t1.start();
        t2.start();
    }
}

class Bank1 implements Runnable {

    private int account = 0;

    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        //方式一
//        for (int i = 0; i < 3; i++) {
//            synchronized (Bank1.class) {
//                account += 1000;
//                System.out.println(account);
//            }
//        }
        //方式二
        for (int i = 0; i < 3; i++) {
            try {
                lock.lock();
                account += 1000;
                System.out.println(Thread.currentThread().getName() + account);
            } finally {
                lock.unlock();
            }
        }
    }
}
